package cn.xfdzx.android.apps.shop.adapter;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.luck.picture.lib.tools.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.MainActivity;
import cn.xfdzx.android.apps.shop.bean.AddressListBeen;
import cn.xfdzx.android.apps.shop.bean.OrderDetailInfoBeen;
import cn.xfdzx.android.apps.shop.evenbus.HomeAdressMessage;
import cn.xfdzx.android.apps.shop.util.ImageGlideUtil;
import cn.xfdzx.android.apps.shop.util.Utils;

import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP;

/**
 * Creator: zf.
 * Date: 2020/7/14.
 * Description:售后服务商品列表适配器.
 */
public class AfterServiceGoodsListAdapter extends BaseQuickAdapter<OrderDetailInfoBeen.DataBean.CartInfoBean, BaseViewHolder>  {
    private OnItemOnClickListener onItemOnClickListener;
    private List<OrderDetailInfoBeen.DataBean.CartInfoBean> cartInfo = new ArrayList<>();//商品列表
    public AfterServiceGoodsListAdapter(@Nullable List<OrderDetailInfoBeen.DataBean.CartInfoBean> data) {
        super(R.layout.item_afterservice_goods, data);
    }

    @Override
    public void setData(int index, @NonNull OrderDetailInfoBeen.DataBean.CartInfoBean data) {
        super.setData(index, data);
    }


    @Override
    protected void convert(final BaseViewHolder viewHolder, final OrderDetailInfoBeen.DataBean.CartInfoBean date) {
        TextView tv_goods_add = viewHolder.getView(R.id.tv_goods_add);
        TextView tv_goods_minus = viewHolder.getView(R.id.tv_goods_minus);
        TextView tv_goods_num = viewHolder.getView(R.id.tv_goods_num);

        ImageView imageView = viewHolder.getView(R.id.iv_shop_picture);
        viewHolder.setText(R.id.tv_shop_goods_name,date.getProductInfo().getStore_name())
        .setText(R.id.tv_shop_goods_price,"￥"+date.getProductInfo().getPrice())
        .setText(R.id.tv_goods_count,"x"+date.getCart_num());

        ImageGlideUtil.loadImage(mContext,imageView,date.getProductInfo().getImage());
        tv_goods_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_goods_minus.setVisibility(View.VISIBLE);
                if (date.getCart_num()>date.getSelGoodsCount()){
                    date.setSelGoodsCount(date.getSelGoodsCount()+1);
                    tv_goods_num.setText(date.getSelGoodsCount()+"");
                    onItemOnClickListener.onItemAddMoveClick(viewHolder.getLayoutPosition(),date.getSelGoodsCount(),date);
                }else {
                    ToastUtils.s(mContext,"超过最大数量");
                    return;
                }
            }
        });

        tv_goods_minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (date.getSelGoodsCount()<=1){
                    date.setSelGoodsCount(0);
                    tv_goods_num.setText("");
                    tv_goods_minus.setVisibility(View.INVISIBLE);
                }else {
                    date.setSelGoodsCount(date.getSelGoodsCount()-1);
                    tv_goods_num.setText(date.getSelGoodsCount()+"");
                }
                onItemOnClickListener.onItemAddMoveClick(viewHolder.getLayoutPosition(),date.getSelGoodsCount(),date);
            }
        });
    }
    public void clearDatas(){
        mData.clear();
        notifyDataSetChanged();
    }
    public void refreshDatas(List<OrderDetailInfoBeen.DataBean.CartInfoBean> list){
        this.cartInfo = list;
        super.replaceData(list);
    }

    public void setItemAddMoveListener(OnItemOnClickListener listener){
        this.onItemOnClickListener = listener;
    }

    public interface OnItemOnClickListener {
        void onItemAddMoveClick(int position,int count, OrderDetailInfoBeen.DataBean.CartInfoBean date);
    }

}
